package com.example.demo.controller;

import com.example.demo.model.Discuss;
import com.example.demo.model.Post;
import com.example.demo.service.DiscussService;
import com.example.demo.service.PostService;
import com.example.demo.common.Result;
import com.example.demo.common.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/post")
@CrossOrigin
public class PostController {
    @Autowired
    private PostService postService;
    @Autowired
    private DiscussService discussService;

    // 查询所有帖子
    @GetMapping()
    public ResponseEntity<Result<List<Post>>> findPost() {
        List<Post> postList = postService.findPost();
        return ResponseEntity.ok(new Result<>(true, StatusCode.OK, "查询成功", postList));
    }

    // 添加帖子
    @PostMapping("/{name}/{title}/{content}")
    public ResponseEntity<Result<Void>> add(@PathVariable("name") String name, @PathVariable("title") String title, @PathVariable("content") String content) {
        Post post = new Post();
        post.setTitle(title);
        System.out.println(title);
        post.setContent(content);
        System.out.println(content);
        System.out.println(name);
        postService.add(post,name);
        return ResponseEntity.ok(new Result<>(true, StatusCode.OK, "添加成功"));
    }

    // 修改帖子
    @PostMapping("/{name}/{title}/{content}/{newtitle}/{newcontent}")
    public ResponseEntity<Result<Void>> updatePost(@PathVariable("name") String name, @PathVariable("title") String oldTitle, @PathVariable("content") String oldContent, @PathVariable("newtitle") String newTitle, @PathVariable("newcontent") String newContent) {

        List<Post> posts = postService.selectByUsername(name);
        for (Post post : posts) {
            if (post.getTitle().equals(oldTitle) && post.getContent().equals(oldContent)) {
                // 找到要更新的帖子
                post.setTitle(newTitle);
                post.setContent(newContent);
                // 执行更新操作
                postService.update(post,post.getPostId());
                return ResponseEntity.ok(new Result<>(true, StatusCode.OK, "修改成功"));
            }
        }
        // 如果未找到要更新的帖子，则返回错误信息
        return ResponseEntity.ok(new Result<>(false, StatusCode.ERROR, "未找到要更新的帖子", null));
    }

    // 删除帖子
    @DeleteMapping("/{deltitle}/{delcontent}/{name}")
    public ResponseEntity<Result<Void>> delete(@PathVariable("deltitle") String deltitle, @PathVariable("delcontent") String delcontent, @PathVariable("name") String name) {
        List<Post> posts = postService.selectByUsername(name);
        for (Post post : posts) {
            if (post.getTitle().equals(deltitle) && post.getContent().equals(delcontent)) {
                // 找到要删除的帖子
                // 执行删除操作
                System.out.println(post);
                postService.delete(post.getPostId());
                return ResponseEntity.ok(new Result<>(true, StatusCode.OK, "删除成功"));
            }
        }
        // 如果未找到要更新的帖子，则返回错误信息
        return ResponseEntity.ok(new Result<>(false, StatusCode.ERROR, "未找到要删除的帖子", null));
    }

    // 根据登录用户查询帖子
    @GetMapping("/{name}/selectByUsername")
    public ResponseEntity<Result<List<Post>>> selectByUsername(@PathVariable("name") String name) {
        List<Post> postList = postService.selectByUsername(name);
        return ResponseEntity.ok(new Result<>(true, StatusCode.OK, "查询成功", postList));
    }

    // 根据帖子标题查询帖子
    @GetMapping("/{title}/selectByTitle")
    public ResponseEntity<Result<Post>> selectByTitle(@PathVariable("title") String title) {
        Post post = postService.selectByTitle(title);
        return ResponseEntity.ok(new Result<>(true, StatusCode.OK, "查询成功", post));
    }

    // 根据id查询帖子
    @GetMapping("/selectById/{id}")
    public ResponseEntity<Result<Post>> selectById(@PathVariable("id") Integer id) {
        Post post = postService.selectById(id);
        return ResponseEntity.ok(new Result<>(true, StatusCode.OK, "查询成功", post));
    }

    // 查询讨论消息
    @GetMapping("/selectByPost/{title}")
    public ResponseEntity<Result<List<Discuss>>> selectByPost(@PathVariable("title") String title) {
        Post post = postService.selectByTitle(title);
        List<Discuss> discussList = discussService.selectByPostId(post.getPostId());
        return ResponseEntity.ok(new Result<>(true, StatusCode.OK, "查询成功", discussList));
    }

    // 添加讨论消息
    @PostMapping("/addByPost/{title}/{content}/{name}")
    public ResponseEntity<Result<Discuss>> addByPost(@PathVariable("title") String title, @PathVariable("content") String content, @PathVariable("name") String name) {
        Discuss discuss = new Discuss();
        Post post = postService.selectByTitle(title);
        discuss.setPostId(post.getPostId());
        discuss.setOwnName(name);
        discuss.setCreateTime(new Date());
        discuss.setContent(content);

        discussService.add(discuss);
        return ResponseEntity.ok(new Result<>(true, StatusCode.OK, "添加成功", discuss));
    }

    // 条件查询所有知识
    @GetMapping("/{keys}")
    public ResponseEntity<Result<List<Post>>> findPostByKeys(@PathVariable String keys) {
        List<Post> postList = postService.findPostByKeys(keys);
        if(postList!=null){
            return ResponseEntity.ok(new Result<>(true, StatusCode.OK, "查询成功", postList));}
        else {
            return ResponseEntity.ok(new Result<>(false, StatusCode.ERROR, "内容被老鼠偷走了...", null));
        }
    }
}
